﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using Model;
using BookManage;

namespace BookManage
{
    public partial class FrmMain : Form
    {
        private int start = 0;//起始页号
        private int totalRows;//总行数
        private int pageCount = 5;//每页显示的行数
        public Book book = null;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//结束整个程序
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            txtCurPage.Text = (start + 1).ToString();
            totalRows = BookServices.GetBookCount();
            lblPage.Text = $"页，共{Math.Ceiling(Convert.ToDouble((double)totalRows / pageCount))}页";
            dgBookList.DataSource = BookServices.GetBookList(start,pageCount);
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void 退出系统ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuStrip1_ItemClicked_1(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        /// <summary>
        /// 图书信息修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            BookUser u = new BookUser();
            if (UserService.GetUserRoleId(u)==2)
            {
                MessageBox.Show("对不起您权限有限无法使用此功能！", "系统提示");
                return;
            }
            frmModifyBook frm = new frmModifyBook();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Book book = frm.book;
                if(BLL.BookServices.ModifyBook(book))
                
                MessageBox.Show("图书信息修改成功！", "系统提示");
            else
                    MessageBox.Show("图书信息修改失败！", "系统提示");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            start = 0;
            dgBookList.DataSource = BookServices.GetBookList(start * pageCount, pageCount);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (start + 1 > totalRows / pageCount)
            {
                MessageBox.Show("已经是最后一页","系统提示");
                return;
            }
            start++;
            dgBookList.DataSource = BookServices.GetBookList(start*pageCount, pageCount);
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (start==0)
            {
                MessageBox.Show("已经是首页了", "系统提示");
                return;
            }
            start--;
            dgBookList.DataSource = BookServices.GetBookList(start * pageCount, pageCount);
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            start= totalRows / pageCount;
            dgBookList.DataSource = BookServices.GetBookList(start * pageCount, pageCount);
        }

        private void txtCurPage_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtPage_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void btnGoto_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtCurPage.Text) > Math.Ceiling(Convert.ToDouble((double)totalRows / pageCount))|| Convert.ToInt32(txtCurPage.Text)<=0)
            {
                MessageBox.Show("没有此页！","系统提示");
                return;
            }
            start = Convert.ToInt32(txtCurPage.Text)-1;
            dgBookList.DataSource = BookServices.GetBookList(start * pageCount, pageCount);
        }

        /// <summary>
        /// 添加图书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 添加图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookUser u = new BookUser();
            if (UserService.GetUserRoleId(u) == 2)
            {
                MessageBox.Show("对不起您权限有限无法使用此功能！", "系统提示");
                return;
            }
            frmAddBook frm = new frmAddBook();
            if(frm.ShowDialog()== DialogResult.OK)
            {
                Book book = frm.book;
                if (BLL.BookServices.AddBook(book))
                    MessageBox.Show("新书入库成功！", "系统提示");
                else
                    MessageBox.Show("新书入库失败！", "系统提示");
            }
        }

        /// <summary>
        /// 删除图书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookUser u = new BookUser();
            if (UserService.GetUserRoleId(u) == 2)
            {
                MessageBox.Show("对不起您权限有限无法使用此功能！", "系统提示");
                return;
            }
            frmDeleteBook frm = new frmDeleteBook();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Book book = frm.book;
                if (BLL.BookServices.DeleteBook(book))
                    MessageBox.Show("删除图书成功！", "系统提示");
                else
                    MessageBox.Show("没有此书，删除失败！", "系统提示");
            }
        }

        /// <summary>
        /// 图书查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frmSearchBook frm = new frmSearchBook();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Book book = frm.book;
                dgBookList.DataSource = BookServices.SearchBook(book);
                if (BLL.BookServices.SearchBook(book) == null)
                    MessageBox.Show("没有此书，查询失败！", "系统提示");
            }
        }

        private void lblPage_Click(object sender, EventArgs e)
        {

        }

        private void dgBookList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
